package thesis.bean;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.validator.ValidatorException;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane.SystemMenuBar;

import org.primefaces.event.RowEditEvent;

import com.sun.org.apache.bcel.internal.generic.NEW;

import thesis.database.Customer;
import thesis.javaclass.SystemModel;

@ManagedBean(name = "customer")
@SessionScoped
public class CustomerBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Customer customer = new Customer();
	private List<Customer> customers;
	private String phoneNumber;
	private boolean hasCus;

	public CustomerBean() {
		customers = SystemModel.getAllCustomers();
		if (customers.size() > 0)
			hasCus = true;
		else
			hasCus = false;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public boolean isHasCus() {
		return hasCus;
	}

	public Customer getCustomer() {
		return customer;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public void setCustomers(ArrayList<Customer> customers) {
		this.customers = customers;
	}

	public List<Customer> getCustomers() {
		return customers;
	}

	public void setCustomers(List<Customer> customers) {
		this.customers = customers;
	}

	public void onEdit(RowEditEvent event) {
		Customer cus = (Customer) event.getObject();

		if (cus == null)
			return;

		cus.setLastUpdate(new Timestamp(new Date().getTime()));

		boolean ok = SystemModel.updateCustomer(cus);
		FacesContext context = FacesContext.getCurrentInstance();

		if (ok) {
			customers = null;
			customers = SystemModel.getAllCustomers();
			context.addMessage(null,
					new FacesMessage("Edited customer " + cus.getCustomerId()
							+ " successfully"));
		} else {
			context.addMessage(null,
					new FacesMessage("Edited customer " + cus.getCustomerId()
							+ " Failing"));
		}
	}

	public void onCancel(RowEditEvent event) {
		FacesMessage msg = new FacesMessage("Editing customer cancelled");
		FacesContext.getCurrentInstance().addMessage(null, msg);
	}

	public void delete() {
		if (customer == null)
			return;

		boolean ok = SystemModel.deleteCustomer(customer.getCustomerId());
		FacesContext context = FacesContext.getCurrentInstance();

		if (ok) {
			customers = null;
			customers = SystemModel.getAllCustomers();

			if (customers.size() > 0)
				hasCus = true;
			else
				hasCus = false;

			context.addMessage(null,
					new FacesMessage("Customer " + customer.getCustomerId()
							+ " deleted!"));
			customer = null;
		} else {
			context.addMessage(null, new FacesMessage(
					"Deleted customer failing"));
		}
	}

	public void findCustomer() {
		customers = null;

		if (phoneNumber.trim().equals("")) {
			customers = SystemModel.getAllCustomers();
		} else {
			customers = SystemModel.getCusBySearch(phoneNumber);
		}

		if (customers.size() == 0)
			hasCus = false;
		else
			hasCus = true;
	}

	public void validateName(FacesContext fc, UIComponent c, Object value) {
		String temp = (String) value;
		if (temp.length() == 0)
			throw new ValidatorException(new FacesMessage(
					"Customer name must have at least 1 character"));
	}

	public void validatePhoneNumber(FacesContext fc, UIComponent c, Object value) {
		String temp = (String) value;
		if (!(temp.matches("[0-9]{10}") || temp.matches("[0-9]{11}")))
			throw new ValidatorException(new FacesMessage(
					"Phone number must have 10-11 numbers"));
	}

}
